/* Wessel van Staal */
/* Configuration file code */

#include "conf.h"
#include "global.h"
#include <stdio.h>
/*
SteelNzb
Copyright (C) 2008  Wessel v. Staal

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <string.h>
#define MAX_LINE 1024

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

#ifdef WIN32
#include <windows.h>
#ifndef WINCE
#include <direct.h>
#endif
#define stricmp _stricmp
#define unlink _unlink
#else
#include <unistd.h>
#include <dirent.h>
#define stricmp strcasecmp
#endif

typedef struct config
{
	config_entry** entries;
	int size;
} config;

void* config_readFile(char* file)
{
	FILE* fp = fopen(file, "r");
	char line[MAX_LINE];
	config* cfg = NULL;
	void* tmp = NULL;
	char* sep = NULL;
	char* crp = NULL;

	if ( fp == NULL )
		return NULL;

	cfg = malloc(sizeof(config));

	if ( cfg == NULL )
	{
		perror("Malloc::ReadConfig");
		exit(1);
	}

	memset(cfg, 0, sizeof(config));

	while ( fgets(line, MAX_LINE, fp) != NULL )
	{
		if ( (sep = strchr(line, '=')) == NULL )
			continue;
		else
		{
			config_entry* entry = NULL;

			if ( (tmp = realloc(cfg->entries, sizeof(config_entry*) * ++cfg->size)) == NULL )
			{
				perror("Realloc::Config");
				exit(1);
			}
			else
				cfg->entries = tmp;

			entry = malloc (sizeof(config_entry));

			if ( entry == NULL )
			{
				perror("Malloc::ReadConfig");
				exit(1);
			}
 
			memset(entry, 0, sizeof(config_entry));

			if ( sep-line > sizeof(entry->key) )
			{
				perror("ConfigFile::Key to large");
				exit(1);
			}

			memcpy(entry->key, line, sep-line);
			memcpy(entry->value, line+(sep-line)+1, sizeof(entry->value));

			if ( (crp = strchr(entry->value, '\r')) != NULL )
				*crp = 0;

			if ( (crp = strchr(entry->value, '\n')) != NULL )
				*crp = 0;

			cfg->entries[cfg->size-1] = entry;
		}
	}
	
	fclose(fp);

	return cfg;
}

config_entry* config_findEntry(void* cfgPtr, char* key)
{
	int i = 0; 
	config* cfg = cfgPtr;

	for ( i=0; i<cfg->size; i++ )
		if ( strncmp(cfg->entries[i]->key, key, strlen(key)) == 0 )
			return cfg->entries[i];

	return NULL;
}

void config_free(void* cfgPtr)
{
	int i =0 ;
	config* cfg = cfgPtr;

	for ( i=0; i<cfg->size; i++ )
		free(cfg->entries[i]);

	free(cfg->entries);
	free(cfg);
}
